#coding:utf8
from __future__ import unicode_literals
from myauth.models import MyGroup,MyUser,PlatForm
from django.db import models
from django.core.urlresolvers import reverse_lazy
import json
from django.forms.models import model_to_dict
from .function import ServerMixin
STATUS_CHOICE = (
        (-1, '未安装'),
        (0, '已经安装'),
        (1, '安装中'),
        (2, '已经合服'),
        (3, '特殊状态'),
    )


# Create your models here.

class Article(models.Model):
    title = models.CharField(u'标题', max_length=256)
    content = models.TextField(u'内容')

    pub_date = models.DateTimeField(u'发表时间', auto_now_add=True, editable=True)
    update_time = models.DateTimeField(u'更新时间', auto_now=True, null=True)

    def __str__(self):  # 在Python3中用 __str__ 代替 __unicode__
        return self.title







# Create your models here.
class Server( models.Model):
    """
    机器信息
    还没实现的
    检查账号和密码，是否可以登录
    检查操作系统版本，和架构，硬件、内存等信息
    添加到errors中去,如果失败，会返回errors
    """
    Machin_status_choice=(
        (0, '正常'),
        (1, '非正常'),
    )
    ip = models.GenericIPAddressField(verbose_name='缺省IP', unpack_ipv4=True, help_text="默认使用的IP，国内一般是电信IP", unique=True)
    ip_extra = models.GenericIPAddressField(verbose_name='第二个IP', unpack_ipv4=True,
                                       help_text="如果不存在，为缺省值0.0.0.0",
                                      default='0.0.0.0')
    ip_inner =  models.GenericIPAddressField(verbose_name='内网IP',
			unpack_ipv4=True,null=False,
			help_text="服务器内网IP地址,必填") #内网IP地址
    port = models.IntegerField('端口', help_text="ssh 端口，默认为22", default=37371) # 触控机器ssh端口是60020
    user = models.CharField('初始登录用户', default='root', max_length=100)
    password = models.CharField('初始密码', max_length=100, default="123456")
    group = models.ForeignKey(MyGroup, verbose_name='代理', max_length=100, default=3)
    platform = models.ForeignKey(PlatForm, verbose_name='游戏平台', max_length=100, blank=True, null=True, default=1)
    # owner editable=False ,表示不可编辑，在createView中处理，需要一个Mixing在form_valid中处理
    owner = models.ForeignKey(MyUser, verbose_name='拥有者', max_length=100, help_text='可以为空，默认由createview处理,暂时可以选择', default=1)
    status = models.IntegerField(verbose_name='环境安装状态', help_text="环境是否安装", choices=STATUS_CHOICE, default=-1)
    create_time = models.DateTimeField('创建时间', auto_now_add=True)
    note = models.TextField('注释', default='', blank=True, null=True)
    machine_room = models.CharField('机房', max_length=100, null=True)
    purpose = models.CharField('用途', max_length=100, null=True)
    Machine_status =  models.IntegerField('状态',choices=Machin_status_choice)

    class Meta:
        verbose_name = '服务器'
        verbose_name_plural = verbose_name
        ordering = ['-id']

    def __str__(self):
        return self.ip


    def toJSON(self):
        fields = []
        for field in self._meta.fields:
            fields.append(field.name)

        d = {}
        for attr in fields:
            d[attr] = getattr(self, attr)

        return json.dumps(d,ensure_ascii=False)


class ServerHardInfo(models.Model):
    ip = models.ForeignKey(Server,verbose_name='服务器IP',unique=True)
    osver = models.CharField('操作系统',blank=True,max_length=100)
    kernel  = models.CharField('内核版本',blank=True,max_length=100)
    cpu_model = models.CharField('cpu模型',blank=True,max_length=100)
    cpu_num = models.IntegerField('逻辑cpu个数',blank=True)
    mem = models.CharField('物理内存',blank=True,max_length=100)
    disk = models.CharField('磁盘',blank=True,max_length=100)
    #Network_Card =models.CharField('网卡类型',blank=True,max_length=100) 
    #Network_MAC = models.CharField('MAC地址',blank=True,max_length=100)
    product = models.CharField('product',blank=True,max_length=100)
    hostname = models.CharField('hostname',blank=True,max_length=100)
    all_info = models.TextField("所有信息",blank=True)
    def __str__(self):
        return self.ip

